//package cn.lx.auth.secutity.config;
//
//import org.apache.commons.logging.LogFactory;
//import org.springframework.beans.factory.BeanClassLoaderAware;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.ApplicationEventPublisher;
//import org.springframework.context.EmbeddedValueResolverAware;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.ImportAware;
//import org.springframework.core.annotation.AnnotationAttributes;
//import org.springframework.core.type.AnnotationMetadata;
//import org.springframework.data.redis.connection.RedisConnection;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.listener.ChannelTopic;
//import org.springframework.data.redis.listener.PatternTopic;
//import org.springframework.data.redis.listener.RedisMessageListenerContainer;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.SchedulingConfigurer;
//import org.springframework.scheduling.config.ScheduledTaskRegistrar;
//import org.springframework.session.MapSessionRepository;
//import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
//import org.springframework.session.data.redis.RedisFlushMode;
//import org.springframework.session.data.redis.RedisOperationsSessionRepository;
//import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction;
//import org.springframework.session.data.redis.config.ConfigureRedisAction;
//import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
//import org.springframework.session.web.http.DefaultCookieSerializer;
//import org.springframework.util.Assert;
//import org.springframework.util.ClassUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.util.StringValueResolver;
//
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.Executor;
//
///**
// * @ClassName RedisHttpSessionConfiguration
// * @Description: //todo
// * @Author: 落雪
// * @CreateDate: 2025/7/14 23:18
// * @UpdateUser: 落雪
// * @UpdateDate: 2025/7/14 23:18
// * @UpdateRemark:
// * @Version: 1.0
// */
//@Configuration
//@EnableScheduling
//public class RedisHttpSessionConfiguration extends SpringHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware, SchedulingConfigurer {
//    @Value("${security.type:}")
//    private String sessionType;
//    static final String SESSION_TYPE_SSO = "hsa-sso";
//    @Autowired
//    private SessionConfig sessionConfig;
//    static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
//    private Integer maxInactiveIntervalInSeconds = 1800;
//    private String redisNamespace = "spring:session";
//    private RedisFlushMode redisFlushMode;
//    private String cleanupCron;
//    private ConfigureRedisAction configureRedisAction;
//    private RedisConnectionFactory redisConnectionFactory;
//    private RedisSerializer<Object> defaultRedisSerializer;
//    private ApplicationEventPublisher applicationEventPublisher;
//    private Executor redisTaskExecutor;
//    private Executor redisSubscriptionExecutor;
//    private ClassLoader classLoader;
//    private StringValueResolver embeddedValueResolver;
//
//    public RedisHttpSessionConfiguration() {
//        this.redisFlushMode = RedisFlushMode.ON_SAVE;
//        this.cleanupCron = "0 * * * * *";
//        this.configureRedisAction = new ConfigureNotifyKeyspaceEventsAction();
//    }
//
//    @Bean
//    @ConditionalOnProperty(
//            name = {"security.type"},
//            havingValue = "hsa-close",
//            matchIfMissing = true
//    )
//    public MapSessionRepository mapSessionRepository() {
//        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
//        defaultCookieSerializer.setCookieName("JSESSIONID");
//        MapSessionRepository mapSessionRepository = new MapSessionRepository(new ConcurrentHashMap());
//        return mapSessionRepository;
//    }
//
//    @Bean
//    @ConditionalOnProperty(
//            name = {"security.type"},
//            havingValue = "hsa-mock",
//            matchIfMissing = true
//    )
//    public MapSessionRepository mockSessionRepository() {
//        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
//        defaultCookieSerializer.setCookieName("JSESSIONID");
//        MapSessionRepository mapSessionRepository = new MapSessionRepository(new ConcurrentHashMap());
//        return mapSessionRepository;
//    }
//
//    @Bean
//    @ConditionalOnProperty(
//            name = {"security.type"},
//            havingValue = "hsa-sso",
//            matchIfMissing = true
//    )
//    public RedisOperationsSessionRepository sessionRepository() {
//        RedisTemplate<Object, Object> redisTemplate = this.createRedisTemplate();
//        RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(redisTemplate);
//        sessionRepository.setApplicationEventPublisher(this.applicationEventPublisher);
//        if (this.defaultRedisSerializer != null) {
//            sessionRepository.setDefaultSerializer(this.defaultRedisSerializer);
//        }
//
//        sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
//        if (StringUtils.hasText(this.redisNamespace)) {
//            sessionRepository.setRedisKeyNamespace(this.redisNamespace);
//        }
//
//        sessionRepository.setRedisFlushMode(this.redisFlushMode);
//        int database = this.resolveDatabase();
//        sessionRepository.setDatabase(database);
//        return sessionRepository;
//    }
//
//    @Bean
//    public RedisMessageListenerContainer redisMessageListenerContainer() {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        if ("hsa-sso".equals(this.sessionType)) {
//            container.setConnectionFactory(this.redisConnectionFactory);
//            if (this.redisTaskExecutor != null) {
//                container.setTaskExecutor(this.redisTaskExecutor);
//            }
//
//            if (this.redisSubscriptionExecutor != null) {
//                container.setSubscriptionExecutor(this.redisSubscriptionExecutor);
//            }
//
//            container.addMessageListener(this.sessionRepository(), Arrays.asList(new ChannelTopic(this.sessionRepository().getSessionDeletedChannel()), new ChannelTopic(this.sessionRepository().getSessionExpiredChannel())));
//            container.addMessageListener(this.sessionRepository(), Collections.singletonList(new PatternTopic(this.sessionRepository().getSessionCreatedChannelPrefix() + "*")));
//        }
//
//        return container;
//    }
//
//    @Bean
//    public InitializingBean enableRedisKeyspaceNotificationsInitializer() {
//        return new EnableRedisKeyspaceNotificationsInitializer(this.redisConnectionFactory, this.configureRedisAction);
//    }
//
//    public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
//        this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
//    }
//
//    public void setRedisNamespace(String namespace) {
//        this.redisNamespace = namespace;
//    }
//
//    public void setRedisFlushMode(RedisFlushMode redisFlushMode) {
//        Assert.notNull(redisFlushMode, "redisFlushMode cannot be null");
//        this.redisFlushMode = redisFlushMode;
//    }
//
//    public void setCleanupCron(String cleanupCron) {
//        this.cleanupCron = cleanupCron;
//    }
//
//    @Autowired(
//            required = false
//    )
//    public void setConfigureRedisAction(ConfigureRedisAction configureRedisAction) {
//        this.configureRedisAction = configureRedisAction;
//    }
//
//    @Autowired
//    public void setRedisConnectionFactory(@SpringSessionRedisConnectionFactory ObjectProvider<RedisConnectionFactory> springSessionRedisConnectionFactory, ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
//        RedisConnectionFactory redisConnectionFactoryToUse = this.sessionConfig.getRedisConnectionFactory();
//        this.redisConnectionFactory = redisConnectionFactoryToUse;
//    }
//
//    @Autowired(
//            required = false
//    )
//    @Qualifier("springSessionDefaultRedisSerializer")
//    public void setDefaultRedisSerializer(RedisSerializer<Object> defaultRedisSerializer) {
//        this.defaultRedisSerializer = defaultRedisSerializer;
//    }
//
//    @Autowired
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.applicationEventPublisher = applicationEventPublisher;
//    }
//
//    @Autowired(
//            required = false
//    )
//    @Qualifier("springSessionRedisTaskExecutor")
//    public void setRedisTaskExecutor(Executor redisTaskExecutor) {
//        this.redisTaskExecutor = redisTaskExecutor;
//    }
//
//    @Autowired(
//            required = false
//    )
//    @Qualifier("springSessionRedisSubscriptionExecutor")
//    public void setRedisSubscriptionExecutor(Executor redisSubscriptionExecutor) {
//        this.redisSubscriptionExecutor = redisSubscriptionExecutor;
//    }
//
//    public void setBeanClassLoader(ClassLoader classLoader) {
//        this.classLoader = classLoader;
//    }
//
//    public void setEmbeddedValueResolver(StringValueResolver resolver) {
//        this.embeddedValueResolver = resolver;
//    }
//
//    public void setImportMetadata(AnnotationMetadata importMetadata) {
//        Map<String, Object> attributeMap = importMetadata.getAnnotationAttributes(EnableRedisHttpSession.class.getName());
//        AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
//        this.maxInactiveIntervalInSeconds = (Integer)attributes.getNumber("maxInactiveIntervalInSeconds");
//        String redisNamespaceValue = attributes.getString("redisNamespace");
//        if (StringUtils.hasText(redisNamespaceValue)) {
//            this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue);
//        }
//
//        this.redisFlushMode = (RedisFlushMode)attributes.getEnum("redisFlushMode");
//        String cleanupCron = attributes.getString("cleanupCron");
//        if (StringUtils.hasText(cleanupCron)) {
//            this.cleanupCron = cleanupCron;
//        }
//
//    }
//
//    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//        if ("hsa-sso".equals(this.sessionType)) {
//            taskRegistrar.addCronTask(() -> {
//                this.sessionRepository().cleanupExpiredSessions();
//            }, this.cleanupCron);
//        }
//
//    }
//
//    private RedisTemplate<Object, Object> createRedisTemplate() {
//        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate();
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//        if (this.defaultRedisSerializer != null) {
//            redisTemplate.setDefaultSerializer(this.defaultRedisSerializer);
//        }
//
//        redisTemplate.setConnectionFactory(this.redisConnectionFactory);
//        redisTemplate.setBeanClassLoader(this.classLoader);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//    private int resolveDatabase() {
//        if (ClassUtils.isPresent("io.lettuce.core.RedisClient", (ClassLoader)null) && this.redisConnectionFactory instanceof LettuceConnectionFactory) {
//            return ((LettuceConnectionFactory)this.redisConnectionFactory).getDatabase();
//        } else {
//            return ClassUtils.isPresent("redis.clients.jedis.Jedis", (ClassLoader)null) && this.redisConnectionFactory instanceof JedisConnectionFactory ? ((JedisConnectionFactory)this.redisConnectionFactory).getDatabase() : 0;
//        }
//    }
//
//    static class EnableRedisKeyspaceNotificationsInitializer implements InitializingBean {
//        @Value("${security.type:}")
//        private String sessionType;
//        static final String SESSION_TYPE_SSO = "hsa-sso";
//        private final RedisConnectionFactory connectionFactory;
//        private ConfigureRedisAction configure;
//
//        EnableRedisKeyspaceNotificationsInitializer(RedisConnectionFactory connectionFactory, ConfigureRedisAction configure) {
//            this.connectionFactory = connectionFactory;
//            this.configure = configure;
//        }
//
//        public void afterPropertiesSet() throws Exception {
//            if (this.configure != ConfigureRedisAction.NO_OP) {
//                if ("hsa-sso".equals(this.sessionType)) {
//                    RedisConnection connection = this.connectionFactory.getConnection();
//
//                    try {
//                        this.configure.configure(connection);
//                    } finally {
//                        try {
//                            connection.close();
//                        } catch (Exception var8) {
//                            LogFactory.getLog(this.getClass()).error("Error closing RedisConnection", var8);
//                        }
//
//                    }
//
//                }
//            }
//        }
//    }
//}
//
